package com.gxa.controller.yhy.Patient;

import com.github.pagehelper.PageInfo;
import com.gxa.common.R;
import com.gxa.common.Status;
import com.gxa.common.vo.PatientSearch;
import com.gxa.common.vo.PatientVo;
import com.gxa.common.vo.TransCondition;
import com.gxa.entity.SysPatient;
import com.gxa.service.PatientManagementService;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;



@Controller
@Api(value = "患者管理所有操作功能",tags = "患者管理所有操作功能API")
@RequiresRoles(value = {"超级管理员","院长","主任","秘书","助理"},logical = Logical.OR)
@RequestMapping("Patient/management")
public class PatientManagementController {

    @Autowired
    PatientManagementService patientManagementService;

    @RequestMapping(value = "/findPatientList", method = RequestMethod.GET)
    @ApiOperation(value = "展示所有患者信息", notes = "获取患者信息", httpMethod = "GET", response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "代表参数格式不对"),
            @ApiResponse(code = 404, message = "代表找不到此请求"),
            @ApiResponse(code = 500, message = "代表后端代码出现异常"),
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每一页页数", paramType = "query", dataType = "String", required = true)
    })
    @ResponseBody
    /**
    获取所有患者的信息展示在页面
     */
    public R findPatientList(Integer currentPage, Integer pageSize) {

        R r = new R<>();
        if (currentPage != null && pageSize != null) {
            PageInfo<SysPatient> patientList = patientManagementService.getPatientList(currentPage, pageSize);
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(patientList);
        }else {
            r.fromStatus(Status.BAD_PAGE_PARAM);
        }

        return r;
    }

    @RequestMapping(value = "/addPatient", method = RequestMethod.POST)
    @ApiOperation(value = "增加患者信息", notes = "添加患者", httpMethod = "POST", response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "添加成功"),
            @ApiResponse(code = 400, message = "代表参数格式不对"),
            @ApiResponse(code = 404, message = "代表找不到此请求"),
            @ApiResponse(code = 500, message = "代表后端代码出现异常"),
    })
    @ResponseBody
    /**
    添加新的患者信息
     */
    public R addPatient(@RequestBody @ApiParam(name = "newPatient", value = "患者对象", required = true) SysPatient newPatient) {

        return patientManagementService.addPatient(newPatient);
    }

    @RequestMapping(value = "/transPatient", method = RequestMethod.POST)
    @ApiOperation(value = "转交患者", notes = "改变患者负责人", httpMethod = "POST", response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "转交成功"),
            @ApiResponse(code = 400, message = "代表参数格式不对"),
            @ApiResponse(code = 404, message = "代表找不到此请求"),
            @ApiResponse(code = 500, message = "代表后端代码出现异常"),
    })
    @ResponseBody
    /**
      转交患者，改变患者负责人信息
     */

    public R transPatient(@RequestBody@ApiParam(name = "转交患者",value = "选择对应的负责人转交患者",required = true)TransCondition transCondition) {

        R r = new R<>();
        boolean b = patientManagementService.transPatient(transCondition);
        if (b == true) {
            r.fromStatus(Status.SUCCESS);
        }else {
            r.setCode(500);
            r.setMsg("转交失败");
        }
        return r;
    }


    @RequestMapping(value = "/searchPatient",method = RequestMethod.POST)
    @ApiOperation(value = "高级搜索",notes = "通过特定选项查找患者",httpMethod = "POST",response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "代表参数格式不对"),
            @ApiResponse(code = 404, message = "代表找不到此请求"),
            @ApiResponse(code = 500, message = "代表后端代码出现异常"),
    })
    @ResponseBody
    /**
    高级搜查患者
     */
    public R searchPatient(@RequestBody @ApiParam(name = "高级搜索",value = "通过特定的条件查询患者",required = true)PatientSearch patientSearch){

        R r = new R<>();
        if (patientSearch != null) {
            PageInfo<PatientVo> patientListBySearch = patientManagementService.getPatientListBySearch(patientSearch);
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(patientListBySearch);
        }else {
            r.fromStatus(Status.BAD_REQUEST_PARAM);
        }
        return r;


    }

    @RequestMapping(value = "/findPatientByName",method =RequestMethod.POST )
    @ApiOperation(value = "通过名字查询患者信息",notes = "查询患者信息",httpMethod = "POST",response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "代表参数格式不对"),
            @ApiResponse(code = 404, message = "代表找不到此请求"),
            @ApiResponse(code = 500, message = "代表后端代码出现异常"),
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每一页页数", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "name", value = "患者姓名", paramType = "query", dataType = "String", required = true)
    })

    @ResponseBody
    /**
     * 按名字查询患者信息
     */
    public R findPatientByName(@RequestParam("name") String name,Integer currentPage, Integer pageSize){
        R r = new R<>();
        if (name != null) {
            PageInfo<SysPatient> patientListByName = patientManagementService.getPatientListByName(name,currentPage,pageSize);
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(patientListByName);
        }else {
            r.fromStatus(Status.BAD_QUERY_PARAM);
        }
        return r;
    }

    /**
     * 修改患者信息
     * @param patient
     * @return
     */
    @RequestMapping(value = "/updatePatient",method =RequestMethod.POST )
    @ApiOperation(value = "修改患者信息",notes = "对患者某些信息进行修改",httpMethod = "POST",response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "修改成功"),
            @ApiResponse(code = 400, message = "代表参数格式不对"),
            @ApiResponse(code = 404, message = "代表找不到此请求"),
            @ApiResponse(code = 500, message = "代表后端代码出现异常"),
    })
    @ResponseBody
    /**
    更新患者信息
     */
    public R updatePatient(@RequestBody@ApiParam(name = "patient",value ="更新患者的信息",required = true )SysPatient patient){
        R r =new R<>();
        if(patient !=null){
            patientManagementService.updatePatient(patient);
            return R.ok();
        }else{
            r.setCode(400);
            r.setMsg("更新失败");
        }

        return r;
    }
    @RequestMapping(value = "/deletePatient",method = RequestMethod.POST)
    @ApiOperation(value = "删除患者信息",notes = "删除患者的信息",httpMethod = "POST",response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功"),
            @ApiResponse(code = 400, message = "代表参数格式不对"),
            @ApiResponse(code = 404, message = "代表找不到此请求"),
            @ApiResponse(code = 500, message = "代表后端代码出现异常"),
    })
    @ApiImplicitParam(name = "id", value = "患者ID", paramType = "query", dataType = "String", required = true)
    @ResponseBody
    /**
    删除患者信息
     */
    public R deletePatient(@RequestParam("id") Integer id){
        return patientManagementService.delPatient(id);
    }
    @RequestMapping(value = "/getPatientById",method = RequestMethod.GET)
    @ApiOperation(value = "通过患者id查询信息",notes = "通过患者id查询信息",httpMethod = "GET",response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "代表参数格式不对"),
            @ApiResponse(code = 404, message = "代表找不到此请求"),
            @ApiResponse(code = 500, message = "代表后端代码出现异常"),
    })
    @ResponseBody
    /**
     根据id查询患者信息
     */
    public R getPatientById(@RequestParam("id") Integer id){
        R r = new R<>();

        if (id != null) {
            PatientVo patientById = patientManagementService.getPatientById(id);

            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(patientById);
        }else {
            r.fromStatus(Status.BAD_QUERY_PARAM);
        }
        return r;
    }





}



